added Feb 2001 SDK
[windows-sources.git] / shared source / vb / language / shared / bcvirtualalloc.h
blobe8f65fd787141929d2be377d7e1571eff501fdfa
1 //============================================================================
2 // A virtual memory allocation wrapper. This is used to make sure that we
3 // don't leak any of this memory.
4 //
5 // This allocator assumes that the memory it was allocated out of is zero
6 // initialized.
7 //============================================================================
9 #pragma once
11 struct BCVirtualAlloc
14 // Constructors/Destructors.
17 BCVirtualAlloc();
18 ~BCVirtualAlloc();
21 // Helpers to make sure that we don't try to allocate bad sizes.
24 // Get the page size.
25 static size_t CbPageSize()
27 return m_cbSizePage;
30 // Adjust the size of memory to reserve.
31 static
32 size_t CbSizeReserve(size_t cbSize)
34 // Make sure we always alloc something.
35 cbSize = cbSize ? cbSize : 1;
37 return(cbSize + m_cbAllocationGranularity - 1)
38 & ~(m_cbAllocationGranularity - 1);
41 // Adjust the size of memory to allocate.
42 static
43 size_t CbSizeAlloc(size_t cbSize)
45 // Make sure we always alloc something.
46 cbSize = cbSize ? cbSize : 1;
47 return(cbSize + m_cbSizePage - 1) & ~(m_cbSizePage - 1);
50 // Minumum allocation granularity.
51 static
52 size_t CbAllocationGranularity()
54 return m_cbAllocationGranularity;
58 // Address space manipulation methods.
61 // Reserve some address space to allocate in.
62 void * ReserveAddressSpace(size_t cbSize);
64 // Unreserve the space.
65 void FreeAddressSpace(void * pv);
67 // Commit memory inside of an address space.
68 void CommitMemory(
69 void * pv,
70 size_t cbSize);
72 // Uncommit the space.
73 void DecommitMemory(
74 void * pv,
75 size_t cbSize);
77 // Change the read-onlyness of a page of memory.
78 void ProtectMemory(
79 void * pv,
80 size_t cbSize,
81 DWORD dwPageAccess);
83 #if DEBUG
84 // Reset the passcount on a chunk of memory.
85 void DebMarkMemory(void * pv);
86 #else !DEBUG
87 void DebMarkMemory(void * pv)
90 #endif !DEBUG
92 private:
94 // The following members are used to determine the size of
95 // the pages we allocate.
97 static size_t m_cbSizePage;
98 static size_t m_cbAllocationGranularity;
100 #if DEBUG
102 unsigned m_passCount;
104 // This structure holds the information about the allocated
105 // memory spaces.
107 struct AddressSpace
109 AddressSpace *m_pNextSpace; // Next field in the linked list.
111 void *m_pvAddressSpace; // The address of the beginning of the space
112 size_t m_cbAddressSpace; // The size of the reserved space
114 size_t m_iPassCount; // Which allocation is this?
117 // List of allocated virutal spaces.
118 AddressSpace *m_pAllocatedAddressSpaces;
120 #endif // DEBUG
125 /* this is an internal class that shouldn't be used directly.
126 Use the TEMPBUF macro instead
128 class TempBuffer {
129 public:
130 TempBuffer(
131 size_t stacksize,
132 size_t size,
133 void * stackbuffer)
135 if (size <= stacksize)
137 m_pcBuffer = stackbuffer;
138 m_where_allocated = LOCAL;
140 else
142 m_pcBuffer = CoTaskMemAlloc(size);
143 m_where_allocated = HEAP;
147 ~TempBuffer()
149 if (m_where_allocated == HEAP) {
150 CoTaskMemFree(m_pcBuffer);
154 void * GetBuffer()
156 return m_pcBuffer;
159 private:
160 enum alloc_kind {LOCAL, HEAP};
161 alloc_kind m_where_allocated;
162 void* m_pcBuffer;
165 /* Macro to declare and efficiently manage a temporary buffer.
166 * Var is the variable name
167 * Type is its type
168 * size is the actual (known at runtime) size of the buffer
169 * Example:
170 * TEMPBUF(tempchars, char*, 2*len(initchars));
172 * NOTE: you must check the pointer to the buffer for NULL in case
173 * the allocator wasn't able to allocate the memory.
175 #pragma warning(disable:6304)//,"It is OK for address-of to be a NOP for temp buffers.")
177 #define TEMPBUF(var,type,size) \
178 char __TempBuf##var[120]; \
179 TempBuffer __TempBuffer##var(120,(size),(char*)&__TempBuf##var); \
180 type var = (type)__TempBuffer##var.GetBuffer();
182 #pragma warning(default:6304)//,"It is OK for address-of to be a NOP for temp buffers.")